﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Edu.Web.Biz;
using Edu.Web.Data.Entity;
using Edu.Web.Data.Service;
using ESoft.Core.Utils.Extensions;

namespace Edu.Web.Controllers
{
    public class NoticeController : BaseController
    {
        private readonly INoticeService _noticeService;
        private readonly ITimeTableService _timeTableService;

        public NoticeController(INoticeService noticeService, ITimeTableService timeTableService)
        {
            _noticeService = noticeService;
            _timeTableService = timeTableService;
        }

        public ActionResult List()
        {
            return View();
        }

        public ActionResult List4Teacher()
        {
            return View();
        }


        public ActionResult CreateNotice()
        {
            return View();
        }

        /// <summary>
        /// 发布通知
        /// </summary>
        /// <param name="noticeVO"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult CreateNotice(NoticeEntity noticeVO)
        {
            try
            {
                if (noticeVO.Title.IsNullOrWhiteSpace())
                {
                    throw new Exception("请输入标题！");
                }
                if (noticeVO.Content.IsNullOrWhiteSpace())
                {
                    throw new Exception("请输入内容！");
                }
                noticeVO.NoticeType = 1;
                noticeVO.CreateTime = DateTime.Now;
                noticeVO.CreateUserId = base.CurrentUser.UserId;
                _noticeService.Add(noticeVO);
                return Content("true");
            }
            catch (Exception e)
            {
                return Content("err:" + e.Message);
            }
        }

        public ActionResult GetList()
        {
            return Json(_noticeService.GetNoticeList(CurrentUser.UserId), JsonRequestBehavior.AllowGet);
        }

        public ActionResult GetAllList()
        {
            var courceIdList = _timeTableService.GetCourceProjectListByStuId(CurrentUser.UserId).Select(x => x.CourceId).Distinct();
            return Json(_noticeService.GetNoticeList().Where(x => x.CourceId == null || (x.CourceId != null && courceIdList.Contains(x.CourceId))), JsonRequestBehavior.AllowGet);
        }

        public ActionResult Delete(long id)
        {
            _noticeService.Delete(id);
            return Content("true");
        }
    }
}